<?php

namespace App\Http\Api\Controllers\Goods;

use App\Exceptions\InvalidRequestException;

use App\Http\Api\Services\ProductListService;
use App\Http\Controllers\Controller;
use App\Http\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use UpdateHelper\NotUpdateInterfaceInstanceException;


class GoodsApiController extends Controller
{
    private $service;

    public function __construct(ProductListService $service)
    {
        $this->service = $service;
    }

    public function listData(Request $request)
    {

        $prod_no   = $request->input('prod_no') ?? '';
        $manu_name = $request->input('manu_name') ?? '';
        $page = $request->input('page_size') ?? 20;
        $type = $request->input('type') ?? 0;

        $data = Product::with('skus');
        if (!empty($prod_no)) {
            $data = $data->where('prod_no','like',"%$prod_no%");
        }
        if (!empty($manu_name)) {
            $data = $data->where('manu_name','like',"%$manu_name%");
        }

        if(!empty($type)){
            $data = $data->orderBy('created_at','desc')->get()->toArray();
        }else{
            $data = $data->orderBy('created_at','desc')->paginate($page);
        }

        return $data;
    }

    public function editGoods(Request $request){
        $prod_no = $request->input('prod_no') ?? '';
        $goods_id = $request->input('goods_id') ?? '';
        $prodImg = $request->input('prodImg') ?? '';
        $descImg = $request->input('descImg') ?? '';
        $third_cid = $request->input('third_cid') ?? '';
        $shop = $request->input('shop') ?? '';

        if(!empty($shop) && !empty($prod_no)){

            Product::where('prod_no',$prod_no)->update(['goods_id'=>$goods_id,'prodImg'=>json_encode($prodImg),'descImg'=>json_encode($descImg),'third_cid'=>$third_cid]);

            DB::table('admin_products_shops')->where('prod_no',$prod_no)->delete();

            $data = [];
            foreach ($shop as $k=>$v){
                $data[$k]['shop_id'] = $v;
                $data[$k]['prod_no'] = $prod_no;
                $data[$k]['created_at'] = date("Y-m-d H:i:s");
            }

            DB::table('admin_products_shops')->insert($data);



        }
        return '';

    }

    public function changeProdData(Request $request){

        $attributes  =   $request->all();
        $res = $this->service->dataChange($attributes);

        return res('修改成功',$res);
    }

    public function delProd(Request $request){

        $attributes  =   $request->all();
        $res = $this->service->delProd($attributes);

        return res('删除成功',$res);
    }

    public function addProd(Request $request){

        $attributes  =   $request->all();
        $res = $this->service->addProd($attributes);

        return res('添加成功',$res);
    }

    public function checkShopProd(Request $request){
        $attributes  =   $request->all();
        $res = $this->service->checkShopProd($attributes);

        return res('检验完成',$res);
    }

}